package yxy.game.pm2.module.gacha;

import org.joda.time.DateTime;
import yxy.apple.util.concurrent.ThreadPoolManager;
import yxy.apple.util.redisson.RedisException;
import yxy.cherry.data.DataCenter;
import yxy.cherry.data.bean.summonpooldate;
import yxy.game.pm2.bean.Currency;
import yxy.game.pm2.bean.Item;
import yxy.game.pm2.bean.currency.CurrencyType;
import yxy.game.pm2.bean.db.dao.MySQLCacheDAO;
import yxy.game.pm2.bean.module.PlayerModule;
import yxy.game.pm2.module.tip.TipHandle;
import yxy.game.pm2.module.tip.TipType;

import java.util.ArrayList;

public class GachaHeroModule extends PlayerModule {

	// bean===========================================================================

	ArrayList<GachaHeroBean> gachaList = new ArrayList<>();

	public ArrayList<GachaHeroBean> getGachaList() {
		return gachaList;
	}

	public void setGachaList(ArrayList<GachaHeroBean> gachaList) {
		this.gachaList = gachaList;
	}

	// func===========================================================================

	public void update() {
		for (GachaHeroBean bean : getGachaList()) {
			bean.update();
		}
		set();
	}

	public GachaHeroBean bean(GachaHeroType type) {
		for (GachaHeroBean bean : getGachaList()) {
			if (bean.getGachaType().equals(type)) {
				return bean;
			}
		}
		return null;
	}

	public void check() {
		ThreadPoolManager.getInstance().execute(()->{
			for (GachaHeroBean bean : getGachaList()) {
				if (bean.getFreeTimes() > 0) {
					new TipHandle().updateTip(getPlayerCode(), TipType.主城_招募建筑, true);
					return;
				}
			}
			try {
				Currency common = Item.get(getPlayerCode(), CurrencyType.CT_物品, 1001); //普通招募令
				Currency advanced = Item.get(getPlayerCode(), CurrencyType.CT_物品, 1002); //高级招募令
				if (common == null){
					return;
				}
				if (advanced == null){
					return;
				}
				if (common.getNumber() >= 10 || advanced.getNumber() >= 10){
					new TipHandle().updateTip(getPlayerCode(), TipType.主城_招募建筑, true);
					return;
				}
			} catch (Exception e) {
				e.printStackTrace();
			}
			new TipHandle().updateTip(getPlayerCode(), TipType.主城_招募建筑, false);
		});
	}

	// redis==========================================================================

	private static String key(String playerCode) {
		return String.format("PLAYER:%s:MODULE:GACHA", playerCode);
	}

	public void set() {
		try {
			setv(key(getPlayerCode()), this);
			check();
		} catch (RedisException e) {
			e.printStackTrace();
		}
	}

	static public GachaHeroModule get(String playerCode) {
		try {
			GachaHeroModule module = getv(key(playerCode));
			if (module == null) {
				module = MySQLCacheDAO.getV(key(playerCode), GachaHeroModule.class);
				if(module != null){module.set();}//把数据同步到redis
			}

			if (module == null) {
				module = create(playerCode);
			}
			if (module == null) return null;

			module.update();
			return module;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}

	static public GachaHeroModule create(String playerCode) {
		DateTime now = DateTime.now();
		GachaHeroModule module = new GachaHeroModule();
		module.setPlayerCode(playerCode);
		for (summonpooldate data : DataCenter.getAllData(summonpooldate.class).values()) {
			if (data.getFreenum() > 0) {
				GachaHeroBean bean = new GachaHeroBean();
				bean.setDataId(data.getId());
				bean.setGachaType(GachaHeroType.forNumber(data.getId()));
				bean.setFreeTimes(data.getFreenum());
				bean.setUpdateTime(now);
				module.getGachaList().add(bean);
			}
		}
		module.set();
		return module;
	}

	//存储到数据库的方法
	public static void saveToDB(String playerCode) {
		try {
			String key = key(playerCode);
			if (touchBucket(key).isExists()) {
				GachaHeroModule gachaHeroModule = getv(key(playerCode));
				if (gachaHeroModule != null) MySQLCacheDAO.setV(key, gachaHeroModule);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}

	}

	//从redis移走数据的操作
	public static void removeAllFromRedis(String playerCode) {
		try {
			touchBucket(key(playerCode)).delete();
		} catch (RedisException e) {
			e.printStackTrace();
		}
	}
	// lock==========================================================================

	@Override
	public String lockKey() {
		return String.format("lock:%s", key(getPlayerCode()));
	}

}
